Add the options from code-blocks to doctest-related directives#14335
Open
ValerianRey wants to merge 5 commits intosphinx-doc:masterfrom
Open
Add the options from code-blocks to doctest-related directives#14335ValerianRey wants to merge 5 commits intosphinx-doc:masterfrom
ValerianRey wants to merge 5 commits intosphinx-doc:masterfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR gives the options of the
.. code-block::directive to the.. doctest::,.. testcode::and.. test_output::directives from thedoctestextension.These options are:
linenoslineno-startemphasize-linesforcecaptionnameclassdedentPurpose
The goal behind this is for users to be able to switch from
code-blocktotestcodeordoctestdirectives extremely easily, so that they can test their documentation's code without losing the super nice display options thatcode-blockoffers. Note that this was a problem that I recently had myself, and that prevented me from usingdoctest.I also think that it makes sense for the
testcode,testoutputanddoctestdirectives to be strongly coupled with thecode-blockdirective (I think we want them to be code blocks with some extra testing), which is why I decided to use inheritance.Implementation details
To avoid code duplication and ensure that the options supported by the doctest-related directives will not diverge from those of
code-blockover time, this makes theTestDirectiveclass extend a newBaseCodeBlockclass, that contains what was inCodeBlock, but without thelanguageargument (language can only be python for doctests, so it doesn't make sense to have a language argument for doctest-related directives).CodeBlockusage should not change at all: it now extendsBaseCodeBlock+ adds thelanguageoptional argument that it already had before, that was stripped fromBaseCodeBlock.There is some special care for when there is a caption: this is because when a caption is provided, the
literal_blockcreated byBaseCodeBlockis wrapped in acontainer. Changes to thisliteral_blockshould thus be made to the wrappedliteral_blockand not to thecontaineritself.Examples
example.rst:built html page (using furo theme):

Disclaimers
<BLANKLINE>. I also used it to generated all of the unit tests. The rest was manually coded, and the architectural decisions were mine.CHANGES.rst. It seems to be only for changes that were already released.doc/usage/extensions/doctest.rst, saying that all of the options fromcode-blockare also available in the doctest directives that build visible code blocks. We could instead list all of those options like the other options of those directives. This would be a bit more work to maintain, because it would lead to some duplication. But it would also make it easier for users to see that these options exist.References